{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if All A's Appears Before All B's"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查是否所有 A 都在 B 之前"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个 <strong>仅</strong> 由字符 <code>'a'</code> 和 <code>'b'</code> 组成的字符串&nbsp; <code>s</code> 。如果字符串中 <strong>每个</strong> <em> </em><code>'a'</code> 都出现在 <strong>每个</strong><em> </em><code>'b'</code><em> </em>之前，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"aaabbb\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "'a' 位于下标 0、1 和 2 ；而 'b' 位于下标 3、4 和 5 。\n",
    "因此，每个 'a' 都出现在每个 'b' 之前，所以返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"abab\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>\n",
    "存在一个 'a' 位于下标 2 ，而一个 'b' 位于下标 1 。\n",
    "因此，不能满足每个 'a' 都出现在每个 'b' 之前，所以返回 false 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"bbb\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "不存在 'a' ，因此可以视作每个 'a' 都出现在每个 'b' 之前，所以返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s[i]</code> 为 <code>'a'</code> 或 <code>'b'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-all-as-appears-before-all-bs](https://leetcode.cn/problems/check-if-all-as-appears-before-all-bs/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-all-as-appears-before-all-bs](https://leetcode.cn/problems/check-if-all-as-appears-before-all-bs/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"aaabbb\"', '\"abab\"', '\"bbb\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        i = 0\n",
    "\n",
    "        while i < len(s) and s[i] == \"a\":\n",
    "            i += 1\n",
    "        if i == len(s):\n",
    "            return True\n",
    "        \n",
    "        while i < len(s) and s[i] == \"b\":\n",
    "            i += 1\n",
    "\n",
    "        return i == len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        loc_a = -1\n",
    "        loc_b = -1\n",
    "        i = 0\n",
    "        for a in s:\n",
    "            if s[i] == \"a\":\n",
    "                loc_a = i\n",
    "            elif s[i] == \"b\" and loc_b == -1:\n",
    "               loc_b = i\n",
    "            i += 1 \n",
    "        if loc_b > loc_a:\n",
    "            return True\n",
    "        elif loc_a == -1 or loc_b == -1:\n",
    "            return True\n",
    "        else:\n",
    "            return False           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        pa = s[::-1].find('a')\n",
    "        if pa==-1:\n",
    "            return True\n",
    "        pa = len(s)-1-pa\n",
    "        pb = s.find('b')\n",
    "        if pb==-1:\n",
    "            return True\n",
    "        if pa<pb:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        for i in range(0, n - 1):\n",
    "            if s[i] == 'b' and s[i + 1] == 'a':\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        a = False\n",
    "        for i in s:\n",
    "            if not a and i == 'b':\n",
    "                a = True\n",
    "            if a:\n",
    "                if i == 'a':\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'b' not in s or 'a' not in s:\n",
    "            return True\n",
    "        return len(s)-1-s[::-1].index('a')<s.index('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if \"ba\" in s:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        counta=s.count('a')\n",
    "        countb=s.count('b')\n",
    "        if s==counta*'a'+countb*'b':\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return list(s) == sorted(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        u=s.count('a')\n",
    "        if 'a' not in s:\n",
    "            return True\n",
    "        else:\n",
    "            for i in range(0,u):\n",
    "                if s[i]=='b':\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        res1=s.rfind(\"a\")\n",
    "        res2=s.find(\"b\")\n",
    "        if res2>res1 or res2==-1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        idxa, idxb = float('-inf'), float('inf')\n",
    "        for k,v in enumerate(s):\n",
    "            if v == 'a': idxa = max(idxa, k)\n",
    "            else: idxb = min(idxb, k)\n",
    "        return idxa < idxb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        have_b = False\n",
    "        for i in range(0, n):\n",
    "            if s[i] == 'b':\n",
    "                have_b = True\n",
    "            elif s[i] == 'a':\n",
    "                if have_b:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        s = list(s)\n",
    "        if 'b' in s:\n",
    "            idx = s.index('b')\n",
    "            for i in range(idx+1,len(s)):\n",
    "                if s[i] == 'a':\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        posa = posb = -1\n",
    "        for i,char in enumerate(s):\n",
    "            if char == \"a\":\n",
    "                posa = i\n",
    "                if (posa > posb) & (posb > -1):\n",
    "                    return False\n",
    "            if (char == \"b\") & (posb == -1):\n",
    "                posb = i\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        if \"a\" not in s:\n",
    "            return True\n",
    "        i = 0\n",
    "        while i < n and s[i] == \"a\":\n",
    "            i += 1\n",
    "        while i < n and s[i] == \"b\":\n",
    "            i += 1\n",
    "        return i == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        label1=0\n",
    "        ls=len(s)\n",
    "        label2=ls-1\n",
    "        co=0\n",
    "        coo=0\n",
    "        for i in range(ls):\n",
    "            if(label2==ls-1 and s[i]=='b' and co==0):\n",
    "                label2=i\n",
    "                co=1\n",
    "            if(label1==0 and s[-i-1]=='a' and coo==0):\n",
    "                label1=ls-i-1\n",
    "                coo=1\n",
    "        return label1<=label2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return s.find('ba') == -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        # 拿到第一个b的下标。\n",
    "        # 从这个位置遍历到尾\n",
    "        # 如果有任何一个a则false。并且停止\n",
    "        try:\n",
    "            tmp_index = s.index(\"b\")\n",
    "        except:\n",
    "            return True\n",
    "        res = True\n",
    "\n",
    "        for i in s[tmp_index:]:\n",
    "            if i == \"a\":\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        flag = 0\n",
    "        for x in s:\n",
    "            if x == 'b':\n",
    "                flag = 1\n",
    "            else:\n",
    "                if flag:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return \"ba\" not in s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s):\n",
    "        if \"ba\" in s: return False\n",
    "        else: return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return \"ba\" not in s or len(set(s)) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'a' not in s:\n",
    "            return True\n",
    "        count = 0\n",
    "        for i in range(len(s)-1):\n",
    "           if s[i] > s[i+1]:\n",
    "               return False\n",
    "        \n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        lasta = -1\n",
    "        firstb = n\n",
    "        for i in range(n):\n",
    "            if s[i] == 'a':\n",
    "                lasta = max(lasta,i)\n",
    "            else:\n",
    "                firstb = min(firstb,i)\n",
    "        return lasta<firstb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        while i < n and s[i] == \"a\":\n",
    "            i += 1\n",
    "        while i < n and s[i] == \"b\":\n",
    "            i += 1\n",
    "        return i == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if all(_ == 'a' for _ in s):\n",
    "            return True\n",
    "        return s.find('b') > s.rfind('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        haveb = False\n",
    "        for i in s:\n",
    "            if i == 'b':\n",
    "                haveb = True\n",
    "            if i == 'a' and haveb:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return ''.join(sorted(s))==s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'a' not in s or 'b' not in s:\n",
    "            return True\n",
    "        dex = s.index(\"b\")\n",
    "        lis = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"a\":\n",
    "                if i > dex:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i] > s[i+1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        '''\n",
    "        haveb = False\n",
    "        for i in s:\n",
    "            if i == 'b':\n",
    "                haveb = True\n",
    "            if i == 'a' and haveb:\n",
    "                return False\n",
    "\n",
    "        return True\n",
    "        '''\n",
    "\n",
    "        return list(s) == sorted(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return list(s)==sorted(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return re.match(r'^a*b*$', s) != None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return s.rfind('a') < s.find('b') or 'b' not in s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        for i in range(len(s)-1):\n",
    "            if (s[i] == \"b\") & (s[i+1] == \"a\"):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        flag = False\n",
    "\n",
    "        for i in s:\n",
    "            if i == \"b\":\n",
    "                flag = True\n",
    "            if i == \"a\" and flag:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return list(s) == sorted(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        # if there is no 'a', return True\n",
    "        if s.count('a') == 0 or s.count('b') == 0:\n",
    "            return True\n",
    "        # find the max index of a and min index of b\n",
    "        \n",
    "        index_a = len(s) - (s[::-1].index('a')) - 1\n",
    "        index_b = s.index('b')\n",
    "        # print(index_a)\n",
    "        # print(index_b)\n",
    "        return index_a < index_b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'b' not in s:\n",
    "            return True\n",
    "        else:\n",
    "            idx = s.find('b')\n",
    "            remain = s[idx+1:]\n",
    "            if 'a' not in remain:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        for i in range(len(s)):\n",
    "            if s[i]==\"b\":\n",
    "                for j in s[i::]:\n",
    "                    if j ==\"a\":\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def checkString(self, s: str) -> bool:\n",
    "#         n=len(s)\n",
    "#         a,b=-1,n\n",
    "#         for i,ch in enumerate(s):\n",
    "#             if ch==\"a\":\n",
    "#                 a=i \n",
    "#             else:\n",
    "#                 if b>i:\n",
    "#                     b=i \n",
    "#         return b-a>0\n",
    "\n",
    "\n",
    "### 官2：str.find\n",
    "class Solution:\n",
    "    def checkString(self,s):\n",
    "        return s.find(\"ba\")==-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        b = 0\n",
    "        for i in s:\n",
    "            if i == 'b':\n",
    "                b = 1\n",
    "            if (i == 'a') and (b == 1):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i] > s[i+1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        ib = len(s)\n",
    "        for i, q in enumerate(s):\n",
    "            if q == 'b':\n",
    "                ib = min(ib, i)\n",
    "            if q == 'a' and i > ib:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'a' not in s or 'b' not in s:\n",
    "            return True\n",
    "        return s.rfind('a') < s.find('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        change = 0\n",
    "        c = 'a'\n",
    "        for i in range(len(s)):\n",
    "            if c != s[i]:\n",
    "                change += 1\n",
    "                if change >= 2:\n",
    "                    return False\n",
    "                c = s[i]\n",
    "        return True\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return \"ba\" not in s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        flag = True\n",
    "        for word in s:\n",
    "            if word==\"b\": \n",
    "                flag=False\n",
    "            if word=='a' and flag==False:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'b' not in s:\n",
    "            return True\n",
    "        for i in s[s.index('b'):]:\n",
    "            if i=='a':\n",
    "                return  False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return \"ba\" not in s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return s.find('ba') == -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        bong = ''\n",
    "        for data in s:\n",
    "            if data == 'a' and bong == 'b' :\n",
    "                return False\n",
    "            elif data == 'b':\n",
    "                bong = 'b'\n",
    "        return True\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return s.find(\"ba\") == -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return \"ba\" not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        a,b =0,0\n",
    "        for c in s:\n",
    "            if c =='a':\n",
    "                if b:\n",
    "                    return False\n",
    "                a+=1\n",
    "            else:\n",
    "                b+=1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        idxa, idxb = float('-inf'), float('inf')\n",
    "        for k,v in enumerate(s):\n",
    "            if v == 'a': idxa = max(idxa, k)\n",
    "            else: idxb = min(idxb, k)\n",
    "        return idxa < idxb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "\n",
    "        left, right = 0, len(s)-1\n",
    "\n",
    "        if len(set(s)) == 1:\n",
    "            return True\n",
    "\n",
    "        while right > 0:\n",
    "            if s[right] == 'a':\n",
    "                break\n",
    "            else:\n",
    "                right -= 1\n",
    "\n",
    "        while right > left:\n",
    "            if s[left] == 'b':\n",
    "                return False\n",
    "            left += 1\n",
    "        \n",
    "        return True\n",
    "\n",
    "        # from the last a, start to go left to find if there is an 'b'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        a=0\n",
    "        for i in s:\n",
    "            if i=='b':\n",
    "                a=1\n",
    "            if i=='a' and a==1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return \"ba\" not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        find=False\n",
    "        for ch in s:\n",
    "            if ch==\"b\":\n",
    "                find=True\n",
    "            if find and ch==\"a\":\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        idxa, idxb = float('-inf'), float('inf')\n",
    "        for k,v in enumerate(s):\n",
    "            if v == 'a': idxa = max(idxa, k)\n",
    "            else: idxb = min(idxb, k)\n",
    "        return idxa < idxb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i] == 'b' and s[i+1] == 'a':\n",
    "                return False\n",
    "        return True\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        bAppear = False\n",
    "        for i in s:\n",
    "            if i == 'a' and bAppear:\n",
    "                return False\n",
    "            if i == 'b':\n",
    "                bAppear = True\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        a_index = -1\n",
    "        b_index = len(s)\n",
    "        for i, ss in enumerate(s):\n",
    "            if ss == 'a':\n",
    "                a_index = i\n",
    "            else:\n",
    "                b_index = min(b_index, i)\n",
    "        return a_index < b_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return list(s) == sorted(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        if 'a' not in s or 'b' not in s:\n",
    "            return True\n",
    "        return s.rfind('a') < s.find('b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        meet_a = False\n",
    "\n",
    "        for i in range(len(s) -1 ,-1,-1):\n",
    "            if s[i] == 'a':\n",
    "                meet_a = True\n",
    "            elif s[i] == 'b':\n",
    "                if meet_a:\n",
    "                    return False\n",
    "        \n",
    "        return not meet_a if meet_a == False else meet_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        index=-1\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='b':\n",
    "                index=i\n",
    "                break\n",
    "        if index==-1:\n",
    "            return True\n",
    "        for j in range(index,len(s)):\n",
    "            if s[j]=='a':\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return s.count('a') == s.rfind('a') + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "      flag = False\n",
    "      for ch in s:\n",
    "        if ch == \"b\":\n",
    "          flag = True\n",
    "        elif flag:\n",
    "          return False\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        # 题目可转化为第一个b之后是否存在a，若存在则False\n",
    "        flag = 1\n",
    "        start = 1\n",
    "        for i in range(len(s)):\n",
    "            if not start and s[i] == 'a':\n",
    "                flag = 0\n",
    "                break\n",
    "            if start and s[i] == 'b':\n",
    "                start = 0\n",
    "        \n",
    "        return flag == 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkString(self, s: str) -> bool:\n",
    "        return list(s) == sorted(s)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
